#include "edituserinfoform.h"

#include "ui_edituserinfoform.h"

#include <Application.h>
#include <CommonUtils.h>
#include <QDateEdit>
#include <QFileDialog>
#include <QMessageBox>
#include <UpLoadFile.h>
#include <data/UserInfo.pb.h>
#include <method/Method.pb.h>
#include <method/Request.pb.h>
#include <method/Response.pb.h>

using namespace proto;

EditUserInfoForm::EditUserInfoForm(QWidget *parent)
    : BasicWindows(parent), ui(new Ui::EditUserInfoForm) {
  ui->setupUi(this);
  // initTitleBar();

  loadUserInfo();
  calenderWidget = new QCalendarWidget(this);
  calenderWidget->resize(QSize(300, 200));
  calenderWidget->hide();
  // 时间控件点击事件
  connect(calenderWidget, SIGNAL(clicked(QDate)), this, SLOT(setBirthday()));

  connect(ui->btn_save, &QPushButton::clicked, this,
          &EditUserInfoForm::onButtonSaveClicked);
  connect(ui->btn_cancel, &QPushButton::clicked, this,
          &EditUserInfoForm::onButtonCancelClicked);
  connect(ui->label_headshot, &QClickLabel::clicked, this,
          &EditUserInfoForm::onHeadshotClicked);
  connect(ui->label_birthday, &QClickLabel::clicked, this,
          &EditUserInfoForm::onBirthdayClicked);
  connect(Application::instance(), &Application::signalSetUserInfo, this,
          &EditUserInfoForm::onSetUserInfo);
}

EditUserInfoForm::~EditUserInfoForm() { delete ui; }

void EditUserInfoForm::loadUserInfo() {
  info = Application::Myself();
  ui->label_headshot->setHeadshot(info.headshot().c_str());
  ui->edit_name->setText(info.name().c_str());
  ui->edit_sign->setText(info.sign().c_str());
  ui->comboBox_sex->setCurrentText(info.sex().c_str());
  ui->edit_address->setText(info.address().c_str());
  ui->edit_emai->setText(info.address().c_str());
  ui->edit_phone->setText(info.phonenumber().c_str());
  ui->label_birthday->setText(info.birthday().c_str());
}

void EditUserInfoForm::onButtonSaveClicked() {
  *info.mutable_headshot() = ui->label_headshot->getHeadshot().toStdString();
  *info.mutable_name() = ui->edit_name->text().toStdString();
  *info.mutable_sign() = ui->edit_sign->text().toStdString();
  *info.mutable_sex() = ui->comboBox_sex->currentText().toStdString();
  *info.mutable_address() = ui->edit_address->text().toStdString();
  *info.mutable_email() = ui->edit_emai->text().toStdString();
  *info.mutable_phonenumber() = ui->edit_phone->text().toStdString();
  *info.mutable_birthday() = ui->label_birthday->text().toStdString();

  UpLoadFile *up = new UpLoadFile();
  connect(up, &UpLoadFile::finished, [=](QString url) {
    qDebug() << "uploadfile: "<< url;
    *info.mutable_headshot() = url.toStdString();
    Application::instance()->SetUserInfo(info);
  });
  up->put(ui->label_headshot->getHeadshot());
}

void EditUserInfoForm::onSetUserInfo(int code, QString message) {
  loadUserInfo();

  if (code == 0) {
    QMessageBox::information(this, "通知", message);
  } else {
    QMessageBox::warning(this, "通知", message);
  }
}

void EditUserInfoForm::onHeadshotClicked() {
  QString filename = QFileDialog::getOpenFileName();
  ui->label_headshot->setHeadshot(filename);

  // Application::instance()->UploadFile(filename);
}

void EditUserInfoForm::onBirthdayClicked() {
  calenderWidget->setVisible(!calenderWidget->isVisible());
  calenderWidget->setSelectedDate(
      QDate::fromString(info.birthday().c_str(), "yyyy-MM-dd"));
}

void EditUserInfoForm::onButtonCancelClicked() { this->close(); }

void EditUserInfoForm::setBirthday() {
  auto date = calenderWidget->selectedDate();
  *info.mutable_birthday() = date.toString("yyyy-MM-dd").toStdString();
  ui->label_birthday->setText(info.birthday().c_str());
}
